package cn.kuporo.seckill.activity.domain.service.impl;

import cn.kuporo.seckill.activity.domain.event.SeckillActivityEvent;
import cn.kuporo.seckill.activity.domain.model.SeckillActivity;
import cn.kuporo.seckill.activity.domain.repository.SeckillActivityRepository;
import cn.kuporo.seckill.activity.domain.service.SeckillActivityDomainService;
import cn.kuporo.seckill.common.constants.MQConstants;
import cn.kuporo.seckill.common.exception.ErrorCode;
import cn.kuporo.seckill.common.exception.SeckillException;
import cn.kuporo.seckill.mq.MessageSenderService;
import com.alibaba.fastjson.JSON;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author: kuporo
 * @since: 2025/7/9
 * @description: 领域层实现类
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class SeckillActivityDomainServiceImpl implements SeckillActivityDomainService {
    private final SeckillActivityRepository seckillActivityRepository;
    private final MessageSenderService messageSenderService;

    @Value("${message.mq.type}")
    private String mqType;
    @Override
    public void saveSeckillActivity(SeckillActivity seckillActivity) {
        log.info("activityPublish|发布秒杀活动|{}", JSON.toJSON(seckillActivity));
        seckillActivityRepository.saveSeckillActivity(seckillActivity);
        log.info("activityPublish|秒杀活动已发布|{}", seckillActivity.getId());
        SeckillActivityEvent seckillActivityEvent = new SeckillActivityEvent(seckillActivity.getId(), seckillActivity.getStatus(), getTopicEvent());
        messageSenderService.send(seckillActivityEvent);
        log.info("activityPublish|秒杀活动事件已发布|{}", JSON.toJSON(seckillActivityEvent));
    }

    /**
     * 获取主题事件
     */
    private String getTopicEvent() {
        return MQConstants.TOPIC_EVENT_ROCKETMQ_ACTIVITY;
    }

    @Override
    public List<SeckillActivity> getSeckillActivityList(Integer status) {
        return seckillActivityRepository.getSeckillActivityList(status);
    }

    @Override
    public List<SeckillActivity> getSeckillActivityListBetweenStartTimeAndEndTime(LocalDateTime currentTime, Integer status) {
        return seckillActivityRepository.getSeckillActivityListBetweenStartTimeAndEndTime(currentTime, status);
    }

    @Override
    public SeckillActivity getSeckillActivityById(Long id) {
        if (id == null){
            throw new SeckillException(ErrorCode.PASSWORD_IS_NULL);
        }
        return seckillActivityRepository.getSeckillActivityById(id);
    }

    /**
     * 修改活动状态
     * @param id 活动id
     * @param status 活动状态
     */
    @Override
    public void updateStatus(Long id, Integer status) {
        log.info("activityPublish|更新秒杀活动状态|活动id:{},状态:{}", id, status);
        seckillActivityRepository.updateStatus(id, status);
        log.info("activityPublish|发布秒杀活动状态事件|活动id:{},状态:{}", status, id);
        SeckillActivityEvent seckillActivityEvent = new SeckillActivityEvent(id, status, getTopicEvent());
        messageSenderService.send(seckillActivityEvent);
        log.info("activityPublish|秒杀活动事件已发布|活动id:{}", id);
    }
}
